package com.administrator.cloudapplication.adapter;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.TextView;

import com.administrator.cloudapplication.Interface.ListItemClickHelp;
import com.administrator.cloudapplication.R;
import com.administrator.cloudapplication.models.EbookModel;
import com.administrator.cloudapplication.models.MediaModel;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2017/4/18.
 */

public class VideoListAdapter extends BaseAdapter {
    private ListItemClickHelp callback;
    private LayoutInflater mInflater;
    private Context mContext;
    private List<MediaModel.DataBean> mListType = new ArrayList<>();
    ArrayList<EbookModel.EbookDataBean> mEbookDataBean = new ArrayList<>();
    private int type;

    public VideoListAdapter(Activity activity, ArrayList<MediaModel.DataBean> mListBean,
                            ArrayList<EbookModel.EbookDataBean> mEbookDataBean, int type,
                            ListItemClickHelp callback) {
        mContext = activity;
        mListType = mListBean;
        this.type=type;
        this.mEbookDataBean=mEbookDataBean;
        mInflater = LayoutInflater.from(activity);
        this.callback = callback;
    }

    @Override
    public int getCount() {
        switch (type) {
            case 0:
                return mListType.size();
            case 1:
                return mEbookDataBean.size();
        }
        return 0;

    }

    @Override
    public Object getItem(int i) {
        switch (type) {
            case 0:
                return mListType.get(i);
            case 1:
                return mEbookDataBean.get(i);
        }
        return null;
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    public class ViewHolder {
        TextView top_left;
        GridView gridView;
        TextView top_right;
    }

    @Override
    public View getView(final int position, View view, final ViewGroup viewGroup) {
        ViewHolder holder;
        if (view == null) {
            view = mInflater.inflate(R.layout.video_item, null);
            holder = new ViewHolder();
            holder.top_left = (TextView) view.findViewById(R.id.video_top_left);
            holder.gridView = (GridView) view.findViewById(R.id.video_grid);
            holder.top_right= (TextView) view.findViewById(R.id.video_top_right);
            view.setTag(holder);
        } else {
            holder = (VideoListAdapter.ViewHolder) view.getTag();
        }
        if(type==0){
            MediaModel.DataBean newsVideoModel = (MediaModel.DataBean) getItem(position);
            holder.top_left.setText(newsVideoModel.getCatename());
            holder.gridView.setNumColumns(2);
            holder.gridView.setAdapter(new VideoGridViewAdapter((Activity) mContext,
                    mListType.get(position).getVideolist(),null,type,callback));
            holder.top_right.setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {
                    callback.onClick(position);
                }
            });
        }
        else {
            EbookModel.EbookDataBean ebookDataBean=(EbookModel.EbookDataBean)getItem(position);
            holder.top_left.setText(ebookDataBean.getCatename());
            holder.gridView.setNumColumns(3);
            holder.gridView.setAdapter(new VideoGridViewAdapter((Activity) mContext,null,
                    mEbookDataBean.get(position).getBooklist(),type,callback));
            holder.top_right.setOnClickListener(new View.OnClickListener() {
                public void onClick(View view) {
                    callback.onClick(position);
                }
            });
        }
        return view;
    }

}
